﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SolucionEntidad;
namespace SolucionDatos
{
    public class ConsultarPermisosPorRolDao
    {

        private SqlConnection cn;

        public ConsultarPermisosPorRolDao(SqlConnection cn) {
            this.cn = cn;
        }

        public List<ConsultarPermisosPorRol> consultarPermisosPorRolDao(ConsultarPermisosPorRolCriteria criteria) {

            List<ConsultarPermisosPorRol> list = new List<ConsultarPermisosPorRol>();
            SqlCommand command = new SqlCommand("select "+ 
                                                   "p.idPermiso,"+
				                                   "p.descripcion,"+
				                                   "p.rutaFormulario,"+
				                                   "p.idModulo,"+
				                                   "pr.idRol "+
                                                   "from permiso p inner join permisoxrol pr" +
			                                       "on p.idPermiso = pr.idPermisos where pr.idRol = @idRol;", this.cn);

            command.Parameters.Add(new SqlParameter("@idRol", criteria.idRol));

            SqlDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                ConsultarPermisosPorRol obj = new ConsultarPermisosPorRol();
                if (dr["idPermiso"] == DBNull.Value) { obj.idPermiso = SqlInt32.Null; } else { obj.idPermiso = (Int32)dr["idPermiso"]; }
                if (dr["descripcion"] == DBNull.Value) { obj.descripcion = SqlString.Null; } else { obj.descripcion = (String)dr["descripcion"]; }
                if (dr["rutaFormulario"] == DBNull.Value) { obj.rutaFormulario = SqlString.Null; } else { obj.rutaFormulario = (String)dr["rutaFormulario"]; }
                if (dr["idModulo"] == DBNull.Value) { obj.idModulo = SqlInt32.Null; } else { obj.idModulo = (Int32)dr["idModulo"]; }
                if (dr["idRol"] == DBNull.Value) { obj.idRol = SqlInt32.Null; } else { obj.idRol = (Int32)dr["idRol"]; }
                list.Add(obj);
            }
            dr.Close();
            return list; 

        }
    }
}
